//
//  UICGPolyline.m
//  MapDirections
//
//  Created by KISHIKAWA Katsumi on 09/08/10.
//  Copyright 2009 KISHIKAWA Katsumi. All rights reserved.
//

#import "UICGPolyline.h"

@implementation UICGPolyline

@synthesize dictionaryRepresentation;
@synthesize routePoints;
@synthesize vertexCount;
@synthesize length;

+ (UICGPolyline *)polylineWithDictionaryRepresentation:(NSDictionary *)dictionary {
	UICGPolyline *polyline = [[UICGPolyline alloc] initWithDictionaryRepresentation:dictionary];
	return [polyline autorelease];
}

- (id)initWithDictionaryRepresentation:(NSDictionary *)dictionary {
	self = [super init];
	if (self != nil) {
		dictionaryRepresentation = [dictionary retain];
        
        routePoints = [NSMutableArray array];
        
        NSArray *steps = [dictionary objectForKey:@"steps"];
        
		for (NSDictionary *step in steps) {
            for (NSDictionary *loc in [step objectForKey:@"lat_lngs"]) {
                CLLocationDegrees latitude  = [[loc objectForKey:@"b"] doubleValue];
                CLLocationDegrees longitude = [[loc objectForKey:@"c"] doubleValue];
                CLLocation *location = [[[CLLocation alloc] initWithLatitude:latitude longitude:longitude] autorelease];
                [routePoints addObject:location];
            }
		}
	}
	return self;
}

- (void)dealloc {
	[dictionaryRepresentation release];
	[vertices release];
	[super dealloc];
}

- (CLLocation *)vertexAtIndex:(NSInteger)index {
	return [routePoints objectAtIndex:index];
}

@end
